package priv.wjh.study.pay.paypal

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.context.annotation.Bean
import org.springframework.util.Base64Utils
import org.springframework.util.LinkedMultiValueMap
import org.springframework.util.MultiValueMap
import org.springframework.web.reactive.function.client.WebClient
import org.springframework.web.reactive.function.client.support.WebClientAdapter
import org.springframework.web.service.invoker.HttpServiceProxyFactory
import spock.lang.Specification


@SpringBootTest
class PaypalClientTest extends Specification {


    PaypalClient paypalClientForTest() {
        WebClient webClient = WebClient.builder()
//                .baseUrl("https://75856cf5-b247-4cf3-acfc-3d3370421a45.mock.pstmn.io")
                .baseUrl("https://api-m.sandbox.paypal.com")
                .build();
        HttpServiceProxyFactory httpServiceProxyFactory =
                HttpServiceProxyFactory.builder(WebClientAdapter.forClient(webClient))
                        .build();
        return httpServiceProxyFactory.createClient(PaypalClient.class);
    }


    PaypalClient paypalClient = paypalClientForTest()

    def "test getToken"() {

        given:
        def authorization = "Basic " + Base64.getEncoder().encodeToString("AW0Y4Y6d2zu8zu3P8WqfioOwsMOgA6lUJjC2ZOcgeb6laagKiJO3_rYZGvGIubtng-DZiQ913vsjBVgM:EGonmxeiLGGWyjGfUOPDB8Sik3rUwoYx3tKxBxyKT28R-m8EzZRvLAJL8xxKdtR2FuMuUxU1B9Kh4SuQ".getBytes())


        MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>();
        parts.add("grant_type", "client_credentials")
        def token = paypalClient.getToken(authorization, parts)
        println(token)
    }
}
